import datetime

from django.db.models import Count

from db.models import XlzClock, XlzIncome, XlzDisburse


def userinfo(queryset, uid):
    # 获取打卡天数
    clock_que = XlzClock.objects.filter(user_id=uid)
    clock = clock_que[0].daka_day if clock_que else 0
    # 获取记账总天数
    now_time = datetime.datetime.now()
    create_time = queryset[0].create_time
    sum_day = now_time-create_time
    # 获取记账总笔数
    income = XlzIncome.objects.filter(user_id=uid)
    disburse = XlzDisburse.objects.filter(user_id=uid)
    income_ = income.aggregate(Count("id"))['id__count'] if income else 0
    disburse_ = disburse.aggregate(Count("id"))['id__count'] if disburse else 0
    sum_account = income_ + disburse_
    return clock, sum_day, sum_account

def daka(queryset, uid):
    # 获取连续打卡天数
    clock_que = XlzClock.objects.filter(user_id=uid).first()
    nowtime = datetime.datetime.now()
    updatetime = clock_que.update_time
    if (nowtime - updatetime).days <= 1:
        clock_que.daka_day += 1
        clock_que.save()
    else:
        clock_que.daka_day = 0
    # 获取记账总天数
    now_time = datetime.datetime.now()
    create_time = queryset[0].create_time
    sum_day = now_time-create_time
    # 获取记账总笔数
    income = XlzIncome.objects.filter(user_id=uid)
    disburse = XlzDisburse.objects.filter(user_id=uid)
    income_ = income.aggregate(Count("id"))['id__count'] if income else 0
    disburse_ = disburse.aggregate(Count("id"))['id__count'] if disburse else 0
    sum_account = income_ + disburse_
    return clock_que.daka_day, sum_day, sum_account